package design.client;
import design.book.*;
import design.builder.Administrator;
import design.builder.BookBuilder;
import design.builder.Builder;
import design.command.*;
import design.decorate.BorrowBook;
import design.power.Users;
import design.power.UsersFactory;

import java.util.*;
public class Client {
	public void init() {
		Scanner scan = new Scanner(System.in);
		Builder build = new BookBuilder();
		Administrator admin = new Administrator();
		admin.setBookBuilder(build);
		Controller control = control();  //命令模式
		Book b,b1;
		BorrowBook bb;
		String temp = "";
		
		
		System.out.println("请问您是否要登陆图书馆管理系统,输入Y进入/其它表示不进入");
		String open = scan.nextLine();
		if(open.equalsIgnoreCase("y")){ //进入图书馆管理系统
			control.readfromfile(null);
			System.out.println("---------欢迎来到图书馆管理系统----------");
			System.out.print("请输入您的身份，包括学生(Student),管理员(Admin),其他身份将无法登陆系统");
			Users user;
			try {
				user = UsersFactory.getUser(scan.nextLine());
				user.operation();
				while(true){
					System.out.println();
					System.out.println("-------------------------请选择您需要的操作--------------------------");
					System.out.println("      1.添加图书           2.查找一本图书        3.查找全部图书");
					System.out.println("4.修改图书     5.按照价格排序图书        6.删除图书         7.借阅图书 ");
					System.out.println("----------------------输入数字8将会退出系统---------------------------");
					String choose = scan.nextLine();
					String BID,multi,name;
					switch(choose){ //功能选择
					case "1": if(user.identity() == 1){
							  System.out.println("请填写待添加图书的信息"); 
							  b = admin.construct(scan); //建造者模式造书
							  control.add(b);
							  System.out.println("请问是否添加相同作者的图书:Y是/其他表示否");
							  multi = scan.nextLine();
							  	if(multi.equalsIgnoreCase("y")){
							  		b1 = b.clone();  //原型模式 克隆对象
							  		System.out.print("请输入相同作者的图书的图书号:"); b1.setBid(scan.nextLine());
							  		System.out.print("请输入相同作者的图书的名字:"); b1.setBname(scan.nextLine());
							  		System.out.print("请输入相同作者的图书出版时间:"); b1.setBpubdate(scan.nextLine());
							  		control.add(b1);  
							  	}
							  	else{
							  		System.out.println("请进行其他操作");
							  	}
					          }
					          else{
					        	  System.out.println("很抱歉，您没有添加图书的权限");
					          }
							  break;
					case "2": System.out.print("请提供查询图书的图书号，形如B0001"); BID = scan.nextLine(); control.searchone(BID); break;
					case "3": control.searchall(null);  break;
					case "4": if(user.identity() == 1){
							  System.out.print("请选择您需要修改的图书的图书号");
							  BID = scan.nextLine();
							  control.modify(BID);
								}
					          else{
					        	  System.out.println("很抱歉，您没有修改图书的权限");
					          }
							  break;
					case "5": control.sortbyprice(null); break;
					case "6": if(user.identity() == 1){
							  System.out.print("请输入您要删除图书的图书号，形如B0001"); 
							  BID = scan.nextLine(); 
							  if(temp.equalsIgnoreCase(BID)){
								  System.out.println("您要删除的图书不存在，请重新操作");
								  break;
							  }
							  temp = BID; 
							  control.delete(temp);  }
								else{
									System.out.println("很抱歉，您没有删除图书的权限");
								}break;
					case "7": System.out.print("请输入您的姓名"); 
							  b = new Book();
							  name=scan.nextLine();  
							  System.out.print("请输入您要借阅的图书的图书号");
							  b.setBid(scan.nextLine());
							  if(temp.equalsIgnoreCase(b.getBid())){
								  System.out.println("您要借阅的图书已被借走，请重新操作");
								  break;
							  }
							  temp = b.getBid();  bb=new BorrowBook(b);  bb.borrow(name);  //装饰模式记录书被借阅的时间和借阅人
							  control.delete(temp);  break;
					case "8": control.writetofile(null); System.out.println("您已退出图书馆管理系统"); scan.close(); System.exit(0);  break;
					default:  System.out.println("您选择的操作有误，请重新选择"); break;
					}
				}
			} catch (Exception e) {
				
				e.getMessage();
				System.out.println("无法登陆图书管理系统");
			}finally{
				scan.close();
			}
			
		}
		else{
			System.out.println("您未进入图书馆管理系统");
			scan.close();
		}
	}
	
	
	public Controller control(){ //命令模式初始化
		AbstractCommand add,searchone,searchall,delete,modify,sortbyprice,write,read;
		add = new AddOperation();
		searchone = new SearchOneOperation();
		searchall = new SearchAllOperation();
		delete = new DeleteOperation();
		modify = new ModifyOperation();
		sortbyprice = new SortOperation();
		write = new WriteOperation();
		read = new ReadOperation();
		Controller control = new Controller(add,searchone,searchall,sortbyprice,modify,delete,write,read);
		
		return control;
	}
}
